package com.tfswx.jkgl.core.cscl;

import cn.hutool.extra.spring.SpringUtil;
import com.tfswx.jkgl.core.bean.Csdy;
import com.tfswx.jkgl.core.cscl.base.CsclBase;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/5
 */
public class JmfsUtil {
    private static final Map<String, CsclBase> jmfsBaseMap = SpringUtil.getBeansOfType(CsclBase.class);

    private static CsclBase getJmfs(String jmfs) {
        if (StringUtils.isBlank(jmfs)) {
            return null;
        }
        String s = String.valueOf(jmfs.charAt(0));
        jmfs = jmfs.replaceFirst(s, s.toLowerCase());
        CsclBase csclBase = jmfsBaseMap.get(jmfs);
        if (csclBase == null) {
            throw new RuntimeException("没有该加密方式");
        }
        return csclBase;
    }

    public static Object encode(Csdy csdy, Object inputDTO, String pBds, Object pInputDTO, boolean sfrc, String id) {
        if (pBds == null) {
            pBds = "";
        }
        if (inputDTO == null) {
            return jmOther(csdy, inputDTO, pInputDTO, pBds, id);
        }
        String cslx = csdy.getCslx();
        if ("List".equals(cslx)) {
            // 是List
            Object o = jmList(csdy, inputDTO, pBds, sfrc, id);
            return jmOther(csdy, o, pInputDTO, pBds, id);
        } else if ("Map".equals(cslx)) {
            // 是Map
            Object o = jmMap(csdy, inputDTO, pBds, sfrc, id);
            return jmOther(csdy, o, pInputDTO, pBds, id);
        } else {
            if (inputDTO instanceof Map) {
                throw new RuntimeException((sfrc ? "入参" : "出参") + (StringUtils.isNotBlank(pBds) ? ("【" + pBds + "】") : "") + "是对象，但配置不是，请检查配置！");
            }
            return jmOther(csdy, inputDTO, pInputDTO, pBds, id);
        }
    }


    public static Object jmOther(Csdy csdy, Object inputDTO, Object pInputDTO, String pBds, String id) {
        String jmfs = csdy.getJmfs();
        if (StringUtils.isBlank(jmfs)) {
            return inputDTO;
        }
        CsclBase csclBase = getJmfs(jmfs);
        if (csclBase == null) {
            return inputDTO;
        }
        return csclBase.encode(inputDTO, csdy.getJmcs(), pBds, id);
    }

    public static Object jmMap(Csdy csdy, Object inputDTO, String pBds, boolean sfrc, String id) {
        List<Csdy> csdy1 = csdy.getCsdy();
        Map<String, Object> resValue = new HashMap<>();
        String csbm = csdy.getCsbm();
        if (!(inputDTO instanceof Map)) {
            throw new RuntimeException((sfrc ? "入参" : "出参") + (csbm != null ? ("【" + csbm + "】") : "") + "不是对象，请检查配置！");
        }

        Map<String, Object> map = (Map<String, Object>) inputDTO;
        for (Csdy child : csdy1) {
            String csbm1 = child.getCsbm();
            Object value = encode(child, map.get(csbm1), StringUtils.isNotBlank(pBds) ? (pBds + "." + csbm1) : csbm1, sfrc ? resValue : inputDTO, sfrc, id);
            resValue.put(csbm1, value);
        }
        return resValue;
    }

    public static Object jmList(Csdy csdy, Object inputDTO, String pBds, boolean sfrc, String id) {
        List<Object> resValue = new ArrayList<>();
        String csbm = csdy.getCsbm();
        if (!(inputDTO instanceof List)) {
            throw new RuntimeException((sfrc ? "入参" : "出参") + (csbm != null ? ("【" + csbm + "】") : "") + "不是列表，请检查配置！");
        }
        // List<Object> value = JSON.parseArray(JSON.toJSONString(inputDTO)).toJavaList(Object.class);
        List<Object> value = (List<Object>) inputDTO;
        String jhlx = csdy.getJhlx();
        for (int i = 0; i < value.size(); i++) {
            Object o = value.get(i);
            if ("Map".equals(jhlx)) {
                Object o1 = jmMap(csdy, o, pBds + "[" + i + "]", sfrc, id);
                Object o2 = jmOther(csdy, o1, null, pBds, id);
                resValue.add(o2);
            } else {
                resValue.add(o);
            }
            // else {
            //     if (o instanceof Map) {
            //         throw new RuntimeException("参数【" + csbm + "】集合类型不匹配，期望（字符串），获得（对象）！");
            //     }
            //     resValue.add(jmOther(csdy, o));
            // }
        }
        return resValue;
    }


    public static Map<String, CsclBase> getJmfsMap() {
        return jmfsBaseMap;
    }
}
